import express from 'express';
import { teachingPointController } from '../controllers/teachingPointController';
import { roleAuth } from '../middleware/roleAuth';
import { Role } from '@prisma/client';
import { validateRequest } from '../middleware/validation';
import { z } from 'zod';

const router = express.Router();

// 验证创建和更新教学点的请求数据
const teachingPointSchema = z.object({
  name: z.string().min(1, '名称不能为空'),
  code: z.string().min(1, '代码不能为空'),
  address: z.string().min(1, '地址不能为空'),
  type: z.string().min(1, '类型不能为空'),
  capacity: z.number().int().positive('容量必须为正整数'),
  status: z.enum(['运营中', '暂停', '关闭']),
  principal: z.string().min(1, '负责人不能为空'),
  contact: z.string().min(1, '联系方式不能为空'),
  description: z.string().optional(),
  facilities: z.array(z.string()).min(1, '至少需要一个设施')
});

// 批量更新状态的请求数据验证
const batchUpdateStatusSchema = z.object({
  ids: z.array(z.string().uuid()).min(1, '至少需要选择一个教学点'),
  status: z.enum(['运营中', '暂停', '关闭'])
});

// 路由配置
router.get('/', roleAuth([Role.ADMIN, 'read:*']), teachingPointController.getTeachingPoints);
router.get('/:id', roleAuth([Role.ADMIN, 'read:*']), teachingPointController.getTeachingPointById);
router.post('/',
  roleAuth([Role.ADMIN]),
  validateRequest(teachingPointSchema),
  teachingPointController.createTeachingPoint
);
router.put('/:id',
  roleAuth([Role.ADMIN]),
  validateRequest(teachingPointSchema),
  teachingPointController.updateTeachingPoint
);
router.delete('/:id', roleAuth([Role.ADMIN]), teachingPointController.deleteTeachingPoint);
router.post('/batch/status',
  roleAuth([Role.ADMIN]),
  validateRequest(batchUpdateStatusSchema),
  teachingPointController.batchUpdateStatus
);

export default router;
